// pages/mycoupon/mycoupon.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['全部', '待使用', '已使用', "已过期"],
    currentTab: 0,
    dcard: [],
    page: 1,
    page_size: 5,
    no_data: 0,
    onearth: false, // 是否已经到达底部
    have: true,
  },
  //响应点击导航栏
  navbarTap: function(e) {
    let that = this;
    that.setData({
      dcard: [],
      page: 1,
      page_size: 5,
      no_data: 0,
      onearth: false,
      have: true,
      currentTab: e.currentTarget.dataset.idx
    });
    if (e.currentTarget.dataset.idx==0){
      that.lists()
    } else if (e.currentTarget.dataset.idx == 1){
      that.lists(1)
    } else if (e.currentTarget.dataset.idx == 2){
      that.lists(2)
    }else{
      that.lists(3)
    }
  },
  // 获取我的优惠券列表
  lists(status) {
    let that = this;
    let page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    if (no_data == 0) {
      wh.Post('/api/mall.coupon/mylist', {
        status: status,
        page: page,
        page_num: page_size,
      }, function(res) {
        console.log('优惠券列表', res)
        let shop = that.data.dcard.concat(res),
          count = res.length;
        if (page_size > count) {
          that.data.no_data = 1;
          if (shop.length > 3) {
            that.setData({
              onearth: true
            })
          }
        }
        that.setData({
          dcard: shop
        })
        if (that.data.dcard.length == 0) {
          that.setData({
            have: false,
          });
        } else {
          that.setData({
            have: true,
          });
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.lists()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let no_data = this.data.no_data,
      currentTab = this.data.currentTab
      page = this.data.page,
      page_size = this.data.page_size;
    if (no_data == 0) {
      this.data.page = page + 1;
      if (currentTab==0){
      this.lists() 
      } else if (currentTab == 1){
        this.lists(1)
      } else if (currentTab ==2) {
        this.lists(2)
      } else if (currentTab == 3) {
        this.lists(3)
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})